var app=getApp();
var req = require("../../utils/req.js");
var url = require("../../utils/url.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sendIntegral: [],
    current_page: 0,
    isLoadedAll: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this;
    that.sendIntegral();
  },
  /**
     * 下拉刷新用户收藏的商品数据
     */
  onPullDownRefresh: function () {
    var that=this;
    if (!that.data.isLoadedAll) {
      that.data.current_page++;
      that.sendIntegral();
    }
    wx.stopPullDownRefresh();
  },
  /**
   * 获取积分记录
   *
   */
  sendIntegral: function () {
    var that = this;
    var storeId = app.globalData.storeId;
    var data = {
      page: that.data.current_page
    };

    req.storeRequest("请稍等", "POST",url.store['getFractionList'], storeId, data, function back(res) {
      var data = res.data;
      if (that.data.sendIntegral.length == 0 && data.length == 0) {
        wx.showToast({
          icon: 'none',
          title: '暂无记录',
        })
        return
      }

      if (data.length != 0) {
        that.data.sendIntegral.push.apply(that.data.sendIntegral, data);
        that.setData({
          sendIntegral: that.data.sendIntegral
        })
        console.log(that.data.current_page)
      } else {
        wx.showToast({
          icon: 'none',
          title: '已经到底了！',
        })
        that.data.isLoadedAll = true; //全部加载完毕
        that.data.current_page = 0;   //当前页面重新设置成1
      }



    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that=this;
    if (!that.data.isLoadedAll) {
      that.data.current_page++;
      that.sendIntegral();
    }
  },
})